package com.example.blogother.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blogother.mapper.UserinfoMapper;
import com.example.blogother.util.MessageUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xpd.blog.bean.Chat;
import com.xpd.blog.bean.Shen;
import com.xpd.blog.bean.Userinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
@RestController
@ServerEndpoint("/tui/{id}")
public class TuiWebSocketController {
    //通过static关键字让userviceImpl变成静态变量属于WebSocketServer类,后面所有的WebSocketServer类的对象共享此变量
    private static UserinfoMapper userinfoMapper;

    @Autowired //将userviceImpl注入到WebSocketServer类的静态变量userviceImpl里
    public void setUserinfoMapper(UserinfoMapper userinfoMapper) {
        TuiWebSocketController.userinfoMapper = userinfoMapper;
    }
    //用来存储每一个客户端对应的ChatController对象
    private static Map<String,TuiWebSocketController> onlines=new ConcurrentHashMap<>();

    //声明一个session对象，用来指定发送消息给指定的用户
    private Session session;
    private Userinfo userinfo;
    @OnOpen
    public void onOpen(Session session,@PathParam("id") String id){
        this.session=session;
        LambdaQueryWrapper<Userinfo> lqw=new LambdaQueryWrapper<>();
        userinfo = userinfoMapper.selectByUsidAfter(Integer.parseInt(id));
        System.out.println("userinfo.getUname() = " + userinfo.getUname());
        //存储当前成功建立连接的用户
        onlines.put(userinfo.getUname(),this);
        String message = MessageUtil.getMessage(true, null, getNames());
       // broadAllUsers(message);

    }
    //推送消息
    private void broadAllUsers(String message){
        //要将该消息推送给所有的用户
        Set<String> strings = onlines.keySet();
        System.out.println("strings = " + strings);
        for (String string : strings) {
            System.out.println("string = " + string);
            TuiWebSocketController chatController = onlines.get(string);
            System.out.println("chatController.getNames() = " + chatController.getNames());
            try {
                chatController.session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private Set<String> getNames(){
        return onlines.keySet();
    }
    @OnClose
    public void OnClose(Session session){

    }
    @OnMessage
    public void OnMessage(Session session,String message) throws IOException {
        Userinfo userinfo1 ;
        System.out.println("message = " + message);
        ObjectMapper objectMapper=new ObjectMapper();
        Shen shen = objectMapper.readValue(message, Shen.class);
        LambdaQueryWrapper<Userinfo> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Userinfo::getStatus,1);
        List<Userinfo> userinfos = userinfoMapper.selectList(lqw);
        userinfo1=  userinfoMapper.selectByUsidAfter(shen.getUsid());
        String resultMessage="";
        if("false".equals(shen.getFlag())){
            resultMessage = MessageUtil.getMessage(false, userinfo1.getUname(), userinfo1.getUname()+"提交了申诉");
            for (Userinfo userinfo : userinfos) {
                onlines.get(userinfo.getUname()).session.getBasicRemote().sendText(resultMessage);
            }
        }else{
            onlines.get(userinfo1.getUname()).session.getBasicRemote().sendText(message);

        }
      /*  onlines.get(userinfo1.getUname()).session.getBasicRemote().sendText(resultMessage);*/

    }

}
